package com.zlq.cn.controller.sign.wnxs;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.config.Config;
import com.zlq.cn.entity.FanyiUser;
import com.zlq.cn.entity.WnxsPoetry;
import com.zlq.cn.service.un.FanyiUserService;
import com.zlq.cn.service.un.WnxsPoetryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2022-03-05
 */
@Api(tags = "为你写诗")
@Validated
@RestController
@RequestMapping("/wnxs/user")
public class WnxsUserController {

    @Resource
    private FanyiUserService fanyiUserService;

    @Resource
    private WnxsPoetryService wnxsPoetryService;

    @ApiOperation("获取服务器时间")
    @GetMapping("/sys/time")
    public SignApiResult<Long> sysTime() {
        return SignApiResult.resultSuccess(System.currentTimeMillis());
    }

    @ApiOperation("用户登录/注册")
    @PostMapping("/login")
    public SignApiResult<FanyiUser> login(@RequestBody Object o) {
        return SignApiResult.resultSuccess(fanyiUserService.login(Config.PROJECT_WNXS));
    }

    @ApiOperation("写诗列表")
    @PostMapping("/list")
    public SignApiResult<Page<WnxsPoetry>> list(@RequestBody WnxsPoetry wnxsPoetry) {
        SignApiBootstrap.requestFanYiUser(Config.PROJECT_WNXS);
        return SignApiResult.resultSuccess(wnxsPoetryService.lambdaQuery()
                .eq(StringUtils.isNotEmpty(wnxsPoetry.getType()), WnxsPoetry::getType, wnxsPoetry.getType())
                .like(StringUtils.isNotEmpty(wnxsPoetry.getTitle()), WnxsPoetry::getTitle, wnxsPoetry.getTitle())
                .like(StringUtils.isNotEmpty(wnxsPoetry.getContent()), WnxsPoetry::getContent, wnxsPoetry.getContent())
                .orderByDesc(WnxsPoetry::getTime)
                .page(new Page<>(1, 20)));
    }

    @ApiOperation("新增写诗")
    @PostMapping("/add")
    public SignApiResult<Boolean> add(@RequestBody WnxsPoetry wnxsPoetry) {
        SignApiBootstrap.requestFanYiUser(Config.PROJECT_WNXS);
        String content = wnxsPoetry.getContent();
        if (StringUtils.isNotEmpty(content) && !content.contains("null")) {
            return SignApiResult.resultSuccess(wnxsPoetryService.save(wnxsPoetry));
        }
        return SignApiResult.resultSuccess(Boolean.TRUE);
    }

    @ApiOperation("修改写诗")
    @PostMapping("/update")
    public SignApiResult<Boolean> update(@RequestBody WnxsPoetry wnxsPoetry) {
        SignApiBootstrap.requestFanYiUser(Config.PROJECT_WNXS);
        return SignApiResult.resultSuccess(wnxsPoetryService.updateById(wnxsPoetry));
    }
}

